local dixin = fk.CreateSkill {
  name = "cf_dixin",
}

Fk:loadTranslationTable {
  ["cf_dixin"] = "地心",
  [":cf_dixin"] = "出牌阶段开始时，你可以选择一项：1.获得当前体力值个【护甲】，然后下个出牌阶段开始你移除所有【护甲】。2.选择一名角色对其造成一点伤害，然后获得【岩脊】标记，本轮结束时，若你拥有【岩脊】标记，你移除该标记对该角色及其上下家各造成一点伤害",
  ["#cf_dixin_choice"] = "获得%arg个护盾，下个出牌阶段开始移除所有护盾",
  ["#cf_dixin"] = "选择一名角色对其造成一点伤害，然后获得【岩脊】标记",
  ["#cf_dixin_damage"] = "选择一名角色造成伤害",
  ["@@cf_dixin"] = "岩脊",
  ["$cf_dixin1"] = "俱收并蓄。",
  ["$cf_dixin2"] = "固若金汤。",
  ["$cf_dixin3"] = "壁立千仞。",
  ["$cf_dixin4"] = "震天撼地。",
}

dixin:addEffect(fk.EventPhaseStart, {
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(dixin.name) and player.phase == player.Play
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if player:getMark(dixin.name) > 0 then
      room:changeShield(player, -player.shield)
      room:setPlayerMark(player, dixin.name, nil)
    end
    return room:askToSkillInvoke(player, { skill_name = dixin.name })
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local shield = math.min(player.hp, 5)
    local choice = room:askToChoice(player, {
      choices = { "#cf_dixin_choice:::" .. shield, "#" .. dixin.name },
    })
    if choice == "#cf_dixin_choice:::" .. shield then
      room:changeShield(player, player.hp)
      room:addPlayerMark(player, dixin.name)
      player:broadcastSkillInvoke(dixin.name, math.random(1, 2))
      room:notifySkillInvoked(player, dixin.name)
    else
      local damage_to = room:askToChoosePlayers(player, {
        targets = room:getAlivePlayers(),
        min_num = 1,
        max_num = 1,
        skill_name = dixin.name,
        prompt = "#cf_dixin_damage",
      })
      player:broadcastSkillInvoke(dixin.name, math.random(3, 4))
      room:notifySkillInvoked(player, dixin.name)
      room:setPlayerMark(player, "cf_dixin_damage", damage_to[1].id)
      room:damage({
        from = player,
        to = damage_to[1],
        damage = 1,
        skillName = dixin.name
      })
      room:addPlayerMark(player, "@@cf_dixin")
    end
  end
})

dixin:addEffect(fk.RoundEnd, {
  can_refresh = function(self, event, target, player, data)
    return player:hasSkill(dixin.name, true) and player:getMark("@@cf_dixin") ~= 0
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local _damage = room:getPlayerById(player:getMark("cf_dixin_damage"))
    local _target = { _damage, room:getPlayerById(_damage:getLastAlive().id), room:getPlayerById(_damage:getNextAlive()
      .id) }
    if _damage:isAlive() then
      for _, p in pairs(_target) do
        room:damage({
          from = player,
          to = p,
          damage = 1,
          skillName = dixin.name
        })
      end
    end
    room:setPlayerMark(player, "cf_dixin_damage", 0)
    room:setPlayerMark(player, "@@cf_dixin", 0)
  end
})

return dixin
